<?php
/**
 * 基类控制器
 * @Author    紫梦
 * @DateTime  2017-11-01
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Log;
use app\common\exception\DefinedException;
use app\common\enums\ResultEnum;
class Base extends Controller
{

    /**
     * 初始化验证登录相关信息
     */
    public function _initialize()
    {
        if(empty(session('admin.id'))){
                $this->error('请先登录',url('index/Login/login'),'',1);
            
        }else{

            //权限验证
            if(!self::juris()){
                $request = Request::instance();

                if($request->isAjax()){
 
                    // throw new DefinedException(ResultEnum::JURIS_ERROR);
                }else{
                   
                    // $this->redirect(url('index/permission/index'),302);
                    
                 // echo "<script>alert('你没有该模块操作权限');top.location.href='".url('index/index')."'</script>";exit;
                }

            }
            self::alilog();
        }
    }

    /**
     * 验证权限
     * @ return boole 
     */
    private static function juris()
    {
        $address = self::address();
        return in_array($address, session('admin.nodes')); 
        
    }

    /**
     * 日志数据
     */
    public static function alilog()
    {
        $address = self::address();
        if(strpos($address,'edit')){
            if(input()){
                Log::record('[ OPERATE ] '.$address.':'.session('admin.id').' '.'data:'.json_encode(input()),'log');
            }
        }else{
            Log::record('[ OPERATE ] '.$address.':'.session('admin.id'),'log');
            
        }
    }

    /**
     *@return   控制器/方法
     */
    private static function address()
    {
        $request = Request::instance();
        $action = $request->action();
        $controller =  strtolower($request->controller());

        return $controller.'/'.$action;
    }


}
